$(function () {
    // 需求1 初始化文章分类列表
    initArtCateList()
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates'
        }).then(res => {
            // console.log(res);
            if (res.data.status != 0) {
                return localStorage.msg(res.data.message)
            }
            let arr = []
            res.data.data.forEach(function (ele) {
                arr.push(`
                  <tr>
                        <td>${ele.Id}</td>
                        <td>${ele.name}</td>
                        <td>${ele.alias}</td>
                        <td>
                            <button data-id="${ele.Id}" class="btn-edit layui-btn layui-btn-xs">编辑</button>
                            <button data-id="${ele.Id}" class="btn-dele layui-btn layui-btn-xs layui-btn-danger">删除</button>
                        </td>
                    </tr>
                `)
            })
            $('tbody').empty().html(arr.join(''))
        })
    }

    //需求2:点击按钮.弹出添加类别对话框
    // 定义一个全局变量  接收open()的索引值  将来用于关闭对话框
    let indexAdd = 0
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '260px'],
            // conten 里面要放form表单  所以放到最末尾
            content: `   
            <form id="addForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">输入框</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">输入框</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class=" layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>`
        })
    })

    // 需求3 添加文章分类-添加的动态元素  需要事件委托
    $('body').on('submit', '#addForm', function (e) {
        // 阻止默认提交
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status != 0) {
                return layer.msg(res.data.message)

            }
            layer.msg('添加成功')
            initArtCateList()

            // 关闭对话框
            layer.close(indexAdd)
        })
    })

    // 4.修改分类弹出层
    let indexEdit = 0
    $('body').on('click', '.btn-edit', function () {
        // 点击编辑按钮有弹出层   1
        indexEdit = layer.open({
            type: 1,
            title: '修改文章类别',
            area: ['500px', '260px'],
            // conten 里面要放form表单  所以放到最末尾
            content: `   
            <form lay-filter="editForm" id="editForm" class="layui-form" action="">
            <!--  隐藏域  填充Id值  将来修改数据的时候使用 -->
             <input name="Id" type="hidden">
                <div class="layui-form-item">
                    <label class="layui-form-label">输入框</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">输入框</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>`
        })

        // 点击编辑按钮赋值       2
        // (获取数据和渲染数据要写到.btn-edit的点击事件里面)
        let id = $(this).attr('data-id')
        //发送ajax
        axios({
            method: 'get',
            // /:id 动态参数(路由参数)
            // 特点:/不能省略   :一定要删除      id是动态变化的
            url: '/my/article/cates/' + id
        }).then(res => {
            // console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layui.form.val('editForm', res.data.data)
        })
    })

    // 需求5  修改文章分类
    $('body').on('submit', '#editForm', function (e) {
        // 阻止默认提交
        e.preventDefault()
        // 修改分类
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status != 0) {
                return layer.msg(res.data.message)

            }
            layer.msg('恭喜你, 修改成功')
            initArtCateList();
            // 关闭对话框
            layer.close(indexEdit)
        })
    })

    // 需求6:删除
    $('tbody').on('click', '.btn-dele', function () {
        // 获取自定义属性值
        let id = $(this).attr('data-id')
        //    弹窗
        //eg1
        layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
            //do something
            // 发送ajax
            axios({
                url: '/my/article/deletecate/' + id,
            }).then(res => {
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)

                }
                layer.msg('恭喜你, 删除成功')
                initArtCateList();
            })
            // 关闭对话框
            layer.close(index);
        });
    })
})